package cn.sh.yhk.quartz.dns;




import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.JsonObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.dnspod.v20210323.DnspodClient;
import com.tencentcloudapi.dnspod.v20210323.models.DescribeRecordListRequest;
import com.tencentcloudapi.dnspod.v20210323.models.DescribeRecordListResponse;
import com.tencentcloudapi.dnspod.v20210323.models.ModifyRecordRequest;
import com.tencentcloudapi.dnspod.v20210323.models.ModifyRecordResponse;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * @Classname Yhk
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/10/1 18:25
 * @Created by y6522
 */
@Service
public class DnsPadServiceImpl implements DnsPadService {

    String myDomain = "iwr.plus";
    String secretId = "AKIDl3NsPzTIytohgXTXl2DOXgiaPYbcWObq";
    String secretKey = "Fd2HNPIBtVB532ta6NfqpokZFwdBbtDP";

    @Override
    public void updateIP(Long recordId) throws TencentCloudSDKException, IOException {
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("dnspod.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        DnspodClient client = new DnspodClient(cred, "", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        ModifyRecordRequest req = new ModifyRecordRequest();
        req.setDomain(myDomain);
        req.setRecordType("A");
        req.setRecordLine("默认");
        req.setValue(getGateIp());
        req.setRecordId(recordId);
        // 返回的resp是一个ModifyRecordResponse的实例，与请求对象对应
        ModifyRecordResponse resp = client.ModifyRecord(req);
        // 输出json格式的字符串回包
        System.out.println(ModifyRecordResponse.toJsonString(resp));
    }


    @Override
    public Long getRecordId() throws TencentCloudSDKException, IOException {
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("dnspod.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        DnspodClient client = new DnspodClient(cred, "", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeRecordListRequest req = new DescribeRecordListRequest();
        req.setDomain(myDomain);
        // 返回的resp是一个DescribeRecordListResponse的实例，与请求对象对应
        DescribeRecordListResponse resp = client.DescribeRecordList(req);
        // 输出json格式的字符串回包
        String respStr = DescribeRecordListResponse.toJsonString(resp);

        JSONArray array = JSONObject.parseObject(respStr).getJSONArray("RecordList");
        for (Object o : array) {
            JSONObject obj = (JSONObject) o;
            if (obj.getString("Name").equals("@") && obj.getString("Type").equals("A")) {
                if (!getGateIp().equals(obj.getString("Value"))) {
                    return obj.getLong("RecordId");
                }

            }
        }
        return null;
    }

    /**
     * 获取公网IP
     *
     * @return
     * @throws IOException
     */
    private static String getGateIp() throws IOException {
        String ip = null;
        BufferedReader br = null;
        try {
            URL url = new URL("http://v6r.ipip.net/?format=callback");
            br = new BufferedReader(new InputStreamReader(url.openStream()));
            String s = "";
            StringBuffer sb = new StringBuffer("");
            String webContent = "";
            while ((s = br.readLine()) != null) {
                sb.append(s + "\r\n");
            }
            webContent = sb.toString();
            int start = webContent.indexOf("(") + 2;
            int end = webContent.indexOf(")") - 1;
            webContent = webContent.substring(start, end);
            ip = webContent;
        } finally {
            if (br != null) {
                br.close();
            }
        }
        if (ip != null && ip != "") {
            throw new RuntimeException();
        }
        return ip;
    }
}
